package com.example.Service.Iml;

import com.example.Mapper.FengShuiMapper;
import com.example.Service.FengShuiService;
import com.example.staticlei.phonenumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.regex.Pattern;

@Service
public class FengShuiServiceIml implements FengShuiService {
    @Autowired
    private FengShuiMapper fengShuiMapper;

    @Override
    public phonenumber getMessage(String number) {
       if (CheckPhoneNumber(number)){
           int id= calculateLuckNumber(number);
          return fengShuiMapper.FengShuiProject(id);
       }else {
           return new phonenumber(0,"手机号码有误，请重新输入",null);
       }
    }

    public static boolean CheckPhoneNumber(String number) {
        if ((number != null) && (!number.isEmpty())) {
            return Pattern.matches("^1[3-9]\\d{9}$", number);
        }
        return false;
    }

    public static int calculateLuckNumber(String phoneNumber) {
        // 提取手机号码的最后四位
        String lastFourDigits = phoneNumber.substring(phoneNumber.length() - 4);
        int baseNumber = Integer.parseInt(lastFourDigits);
        // 步骤2: 除以80
        double result = baseNumber / 80.0;
        int integerPart = (int) result;
        double decimalPart = result - integerPart;
        // 如果结果为0，则吉凶数字为80
        if (integerPart == 0) {
            return 80;
        }
        // 将小数位乘以80
        int luckNumber = (int) (decimalPart * 80);
        return luckNumber;
    }
}